﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Models;
using TugasAkhir.Objects;

namespace TugasAkhir
{
    public partial class Pertanyaan : System.Web.UI.Page
    {
        private DataSet hasilQuery;
        private DosenObject dosen;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            CheckRole();
            if (!Page.IsCallback)
            {
                if (IsSearchMode())
                {
                    this.InitTabelPertanyaan(Request.QueryString["q"]);
                    this.ltKataKunci.Text = Request.QueryString["q"];
                    this.searchLabel.Attributes["class"] = "col-sm-6";
                    this.searchBoxSm.Attributes["class"] = "col-sm-6";
                }
                else
                {
                    this.InitTabelPertanyaan();
                }
            }
        }

        private void CheckRole()
        {
            if (int.Parse(Session["id_role"].ToString().Substring(2)) == 2)
            {
                this.liPertanyaan.Attributes.Remove("hidden");
                this.liPengaturanPertanyaan.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else if (int.Parse(Session["id_role"].ToString().Substring(2)) == 3)
            {
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else
            {
                this.liRekomendasi.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liPengaturanRekomendasi.Attributes.Remove("hidden");
                this.liDaftarRekomendasi.Attributes.Remove("hidden");
            }
        }

        private void CheckSession()
        {
            if (Session["id_role"] != null && Session["id_role"].ToString() == "RL02")
            {
                DosenModel.GetDosenDetail(Session["username"].ToString(), out this.dosen);
                this.litName.Text = dosen.NamaDosen;
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private bool IsSearchMode()
        {
            return Request.QueryString["q"] != null && Request.QueryString["q"] != string.Empty;
        }

        private void InitTabelPertanyaan()
        {
            bool flag = PertanyaanModel.GetSemuaPertanyaan(out hasilQuery);
            if (flag)
            {
                this.gvPertanyaan.DataSource = this.hasilQuery.Tables[0];
                this.gvPertanyaan.DataBind();
            }
        }

        private void InitTabelPertanyaan(string kataKunci)
        {
            bool flag = PertanyaanModel.Pencarian(kataKunci, out hasilQuery);
            if (flag)
            {
                this.gvPertanyaan.DataSource = this.hasilQuery.Tables[0];
                this.gvPertanyaan.DataBind();
            }
        }

        protected void gvPertanyaan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("updateRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.EditCommand(index);
            }
            else if (e.CommandName.Equals("deleteRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DeleteCommand(index);
            }
        }

        private void EditCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfUpdateID);
            var pertanyaan = new PertanyaanObject();

            PertanyaanModel.GetDetailPertanyaan(this.hfUpdateID.Value, out pertanyaan);
            this.txtEditPertanyaan.Text = pertanyaan.Pertanyaan;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowEditScript", "ShowEdit();", true); 
        }

        private void DeleteCommand(int index)
        {
            this.SetHiddenField(index, ref this.hfDeleteID);

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDeleteScript", "ShowDelete();", true); 
        }

        protected void gvPertanyaan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvPertanyaan.PageIndex = e.NewPageIndex;
            if (IsSearchMode())
            {
                this.InitTabelPertanyaan(Request.QueryString["q"]);
            }
            else
            {
                this.InitTabelPertanyaan();
            }

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "InitScript", "Init();", true);
        }

        protected void btCari_ServerClick(object sender, EventArgs e)
        {
            if (this.tbCariSm.Text != string.Empty || this.tbCariXs.Text != string.Empty)
            {
                Response.Redirect("Pertanyaan.aspx?q=" + this.tbCariSm.Text, true);
            }
        }

        protected void btAddPertanyaan_Click(object sender, EventArgs e)
        {
            string idPertanyaan;
            PertanyaanModel.GetId(out idPertanyaan);

            var pertanyaan = new PertanyaanObject();
            pertanyaan.IdPertanyaan = idPertanyaan;
            pertanyaan.Pertanyaan = this.txtAddPertanyaan.Text;

            bool flag = PertanyaanModel.TambahPertanyaan(pertanyaan);
            if (flag)
            {
                if (IsSearchMode())
                {
                    this.InitTabelPertanyaan(Request.QueryString["q"]);
                }
                else
                {
                    this.InitTabelPertanyaan();
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideAddScript", "HideAdd();", true);
            }
        }

        protected void btDeletePertanyaan_Click(object sender, EventArgs e)
        {
            bool flag = PertanyaanModel.HapusPertanyaan(this.hfDeleteID.Value);

            if (flag)
            {
                if (IsSearchMode())
                {
                    this.InitTabelPertanyaan(Request.QueryString["q"]);
                }
                else
                {
                    this.InitTabelPertanyaan();
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideAddScript", "HideDelete();", true);
            }
        }

        protected void SetHiddenField(int index, ref HiddenField hiddenField)
        {
            string code = gvPertanyaan.DataKeys[index].Value.ToString();
            hiddenField.Value = code;
        }

        protected void btEditPertanyaan_Click(object sender, EventArgs e)
        {
            var editedPertanyaan = new PertanyaanObject();
            editedPertanyaan.IdPertanyaan = this.hfUpdateID.Value;
            editedPertanyaan.Pertanyaan = this.txtEditPertanyaan.Text;

            bool flag = PertanyaanModel.UbahPertanyaan(editedPertanyaan);

            if (flag)
            {
                if (IsSearchMode())
                {
                    this.InitTabelPertanyaan(Request.QueryString["q"]);
                }
                else
                {
                    this.InitTabelPertanyaan();
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "HideEditScript", "HideEdit();", true);
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session["id_role"] = "";
            Session["username"] = "";
            CheckSession();
        }
    }
}